#ifndef CIRCLE_HPP
#define CIRCLE_HPP

#include "basic_primitives.hpp"
#include <vl/vec.hpp>

template< typename BitmapT, typename BlitterT >
void circle( BitmapT* bitmap, vl::ivec pos, int radius, typename BlitterT::SrcColorT color, BlitterT& blitter )
{
	int cx = 0;
	int cy = radius;
	int df = 1 - radius; 
	int d_e = 3;
	int d_se = -2 * radius + 5;

	do {
		putPixel( bitmap, vl::ivec(pos.x+cx, pos.y+cy) , color, blitter );
		if (cx) putPixel( bitmap, vl::ivec(pos.x-cx, pos.y+cy), color, blitter );
		if (cy) putPixel( bitmap, vl::ivec(pos.x+cx, pos.y-cy), color, blitter );
		if (cx && cy) putPixel( bitmap, vl::ivec(pos.x-cx, pos.y-cy), color, blitter );
			
		if (cx != cy) {
			putPixel( bitmap, vl::ivec(pos.x+cy, pos.y+cx), color, blitter );
			if (cx) putPixel( bitmap, vl::ivec(pos.x+cy, pos.y-cx), color, blitter );
			if (cy) putPixel( bitmap, vl::ivec(pos.x-cy, pos.y+cx), color, blitter );
			if (cx && cy) putPixel( bitmap, vl::ivec(pos.x-cy, pos.y-cx), color, blitter );
		}
		
		if (df < 0)  {
			df += d_e;
			d_e += 2;
			d_se += 2;
		}
		else { 
			df += d_se;
			d_e += 2;
			d_se += 4;
			cy--;
		} 
		
		cx++; 
		
	} while (cx <= cy);
}


template< typename BitmapT, typename BlitterT >
void circleFill( BitmapT* bitmap, vl::ivec pos, int radius, typename BlitterT::SrcColorT color, BlitterT& blitter )
{
	int cx = 0;
	int cy = radius;
	int df = 1 - radius; 
	int d_e = 3;
	int d_se = -2 * radius + 5;

	do {
		hLine( bitmap, vl::ivec(pos.x-cy, pos.y-cx), 2*cy+1, color, blitter );
		if (cx)	hLine( bitmap, vl::ivec(pos.x-cy, pos.y+cx), 2*cy+1, color, blitter );
		
		if (df < 0)  {
			df += d_e;
			d_e += 2;
			d_se += 2;
		}
		else { 
			if (cx != cy) {
				hLine( bitmap, vl::ivec(pos.x-cx, pos.y-cy), 2*cx+1, color, blitter );
				if (cy) hLine( bitmap, vl::ivec(pos.x-cx, pos.y+cy), 2*cx+1, color, blitter );
			}
			
			df += d_se;
			d_e += 2;
			d_se += 4;
			cy--;
		} 
		
		cx++; 
		
	} while (cx <= cy);
}

#endif // CIRCLE_HPP
